package com.travel.server.manager.impl.command.customer;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.customer.DeleteCustomerInfoRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.model.CustomerInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:05
 * @Desc:
 */
@Component
@Slf4j
public class DeleteCustomerInfoCommand extends AbstractCommand<DeleteCustomerInfoRequest, EmptyResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Override
    protected void checkInput(DeleteCustomerInfoRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(DeleteCustomerInfoRequest request) {
        checkInput(request);

        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        CustomerInfo customerInfo = customerDal.selectById(request.getId());
        customerInfo.setState(CommonConstant.NO);
        customerInfo.setUpdateBy(userInfo.getName());
        customerInfo.setUpdateTime(now);

        customerDal.insertOrUpdate(customerInfo);

        return new EmptyResponse();
    }
}
